<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        :root {
            --main-bg-color: red;
        }

        body {
            background-color: var(--main-bg-color);
        }
    </style>
</head>
<body>
    <button onclick="get()">获取css变量</button></button>
    <script>
        var r = document.querySelector(':root');
        console.log(r);
        function get(){
            let rs=getComputedStyle(r);
            console.log(rs);
            let _css=rs.getPropertyValue('--main-bg-color');
            console.log(_css);
        }
        function set(){
            r.style.setProperty('--main-bg-color','blue');
        }
    </script>
</body>
</html>